package com.tao.controller;

import com.tao.mapper.WeiboMapper;
import com.tao.pojo.dto.CommentWeiboDTO;
import com.tao.pojo.dto.WeiboDTO;
import com.tao.pojo.entity.User;
import com.tao.pojo.entity.Weibo;
import com.tao.pojo.vo.UserVO;
import com.tao.pojo.vo.WeiboDetailVO;
import com.tao.pojo.vo.WeiboListVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RestController
public class WeiboController {
    @Autowired(required = false)
    private WeiboMapper weiboMapper;
    @RequestMapping("/weibo/insert")
    public int insert(@RequestBody WeiboDTO weiboDTO, HttpSession session){
        UserVO userVO = (UserVO) session.getAttribute("user");
        if (userVO==null){
            return 2;
        }
        Weibo weibo = new Weibo();
        BeanUtils.copyProperties(weiboDTO,weibo);
        weibo.setCreated(new Date());
        weibo.setUserId(userVO.getId());
        weiboMapper.insert(weibo);
        return 1;
    }
    @RequestMapping("/weibo/select")
    public List<WeiboListVO> select(){
        return weiboMapper.select();
    }
    @RequestMapping("/weibo/selectById")
    public WeiboDetailVO selectById(int id){
        return weiboMapper.selectById(id);
    }
//    /weibo/select
    @RequestMapping("/weibo/selectSession")
    public int selectSession(int id, HttpSession session){
        UserVO userVO = (UserVO) session.getAttribute("user");
        if (userVO==null){
            return 2;
        }
        if (userVO.getId()== weiboMapper.selectSession(id)){
            return 1;
        }
        return 2;
    }
}
